﻿@using Exhort.Backstage.Components;

@{
    var work = this.GetController();
}

<script type="text/template" id="basicuser-updatepwd-template">
    <div class="page-dialog" style="width:400px;">
        <el-form ref="form" :rules="rules" :model="model" label-width="80px">
            <el-form-item prop="username" label="用户账号">
                <el-input v-model="model.username" disabled></el-input>
            </el-form-item>
            <el-form-item prop="oldpwd" label="原始密码">
                <el-input v-model="model.oldpwd" show-password></el-input>
            </el-form-item>
            <el-form-item prop="newpwd" label="新的密码">
                <el-input v-model="model.newpwd" show-password></el-input>
            </el-form-item>
            <el-form-item prop="newpwd2" label="密码确认">
                <el-input v-model="model.newpwd2" show-password></el-input>
            </el-form-item>
        </el-form>
    </div>
</script>
<script type="text/javascript">
    var basicuserUpdatepwd = {
        template: "#basicuser-updatepwd-template",
        data: function () {
            return {
                model: {
                    id: "@(work.UserID)",
                    username: "@(work.UserName)",
                    oldpwd: "",
                    newpwd: "",
                    newpwd2: ""
                },
                rules: {
                    oldpwd: [
                        { required: true, message: "请填写原始密码", trigger: "blur" },
                        { min: 6, max: 16, message: "密码长度再 6 到 16 个字符之间", trigger: "blur" }
                    ],
                    newpwd: [
                        { required: true, message: "请填写新的密码", trigger: "blur" },
                        { min: 6, max: 16, message: "密码长度再 6 到 16 个字符之间", trigger: "blur" }
                    ],
                    newpwd2: [{ required: true, validator: this.validatorPwd, trigger: "blur" }]
                }
            }
        },
        methods: {
            validatorPwd: function (rule, value, callback) {
                var that = this;
                if (!value) {
                    callback(new Error("请填写密码确认"));
                }
                else if (value !== that.model.newpwd) {
                    callback(new Error("两次密码填写不一致"));
                }
                else { callback(); }
            },
            submitForm: function () {
                var that = this;
                that.$refs.form.validate((valid) => {
                    if (valid) {
                        workVue.ajax({
                            url: "/basic/user/updatepwd",
                            type: "post",
                            data: that.model,
                            target: that.$parent.$el,
                            success: function (result) {
                                workVue.dialog.visible = false;
                                that.$message.success(result.message);
                            }
                        });
                    }
                })
            }
        },
        mounted: function () {
            workVue.dialog.handle = this.submitForm;
        }
    }
</script>